package com.dealin.dealinlibs.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.dealin.dealinlibs.tool.DisplayTool;
import com.dealin.dealinlibs.tool.InputMethodTool;

/**
 * Created by lenovo on 2016/10/9.
 */

public class DLDialog {
    private Context context;
    private AlertDialog.Builder builder;
    private AlertDialog dialog;
    private android.app.AlertDialog dialog1;
    private int themeResId=0;

    private String defaultTitle = "提示";
    private String defaultOKTip = "确定";
    private String defaultNOTip = "取消";
    private String defaultMidTip = "默认";

    public static int DIALOG_EDITTEXU_MARGIN_LEFT=20;
    public static int DIALOG_EDITTEXU_MARGIN_TOP=0;
    public static int DIALOG_EDITTEXU_MARGIN_RIGHT=10;
    public static int DIALOG_EDITTEXU_MARGIN_BOTTOM=0;

    public DLDialog(@NonNull Context context) {
        this.context = context;
        init();

    }

    public DLDialog(@NonNull Context context, @StyleRes int themeResId) {
        this.context = context;
        this.themeResId = themeResId;
        init();
    }

    private void init() {
        if (themeResId == 0) {
            dialog = (builder = new AlertDialog.Builder(context)).create();
        } else {
            dialog=(builder = new AlertDialog.Builder(context,themeResId)).create();
        }
        dialog.setCancelable(false);
    }
    //显示消息的四种方案
    public void showMessege(String msg) {
        init();
        builder.setTitle(defaultTitle);
        builder.setMessage(msg);
        builder.setPositiveButton(defaultOKTip, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * 消息对话框
     * @param title 标题
     * @param msg 消息
     */
    public void showMessege(String title, @NonNull String msg) {
        init();
        builder.setTitle(title == null ? defaultTitle : title);
        builder.setMessage(msg);
        builder.setPositiveButton(defaultOKTip, null);
        builder.show();

    }

    /**
     * 显示消息对话框
     * @param title 标题
     * @param msg 消息
     * @param okTip 确定
     * @param onOkClickListener 当确定点击
     */
    public void showMessege(String title, String msg, String okTip, DialogInterface.OnClickListener onOkClickListener) {
        init();
        builder.setTitle(title == null ? defaultTitle : title);
        builder.setMessage(msg);
        builder.setPositiveButton(okTip == null ? defaultOKTip : okTip, onOkClickListener);
        builder.setNegativeButton(defaultNOTip, null);
        builder.show();

    }

    /**
     * 含确定取消对话框
     * @param title 标题
     * @param msg 消息
     * @param okTip 确定
     * @param onOkClickListener 确定点击时
     * @param cancelTip 取消
     * @param onCancelClickListener 取消点击时
     */
    public void showMessege(String title, @NonNull String msg, String okTip, DialogInterface.OnClickListener onOkClickListener, String cancelTip, DialogInterface.OnClickListener onCancelClickListener) {
        init();
        builder.setTitle(title == null ? defaultTitle : title);
        builder.setMessage(msg);
        builder.setPositiveButton(okTip == null ? defaultOKTip : okTip, onOkClickListener);
        builder.setNegativeButton(cancelTip == null ? defaultNOTip : cancelTip, onCancelClickListener);
        builder.show();
    }

    /**
     * 单输入框对话框
     * @param title 标题
     * @param hint 提示
     * @param onInputCompletedListener 输入完成监听
     */
    public void showInput(String title, String hint,@NonNull final OnInputCompletedListener onInputCompletedListener) {
        init();
        final TextInputLayout textInputLayout = getEditText();
        textInputLayout.setHint(hint);
        builder.setTitle(title);
        builder.setView(textInputLayout);
        builder.setPositiveButton(defaultOKTip, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                onInputCompletedListener.onInputCompleted(new String[]{textInputLayout.getEditText().getText().toString()}, new TextInputLayout[]{textInputLayout});
            }
        });
        builder.setNegativeButton(defaultNOTip, null);
        InputMethodTool.showInputMethod(context,textInputLayout);
        builder.show();
    }

    /**
     * 双输入框对话框
     * @param title 对话框标题
     * @param hint1 提示一
     * @param hint2 提示二
     * @param onInputCompletedListener 输入完成监听
     */
    public void showInput(String title, String hint1, String hint2, @NonNull final OnInputCompletedListener onInputCompletedListener) {
        init();
        final TextInputLayout textInputLayout = getEditText();
        textInputLayout.setHint(hint1);
        final TextInputLayout textInputLayout1 = getEditText();
        textInputLayout1.setHint(hint2);
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(textInputLayout);
        linearLayout.addView(textInputLayout1);
        builder.setTitle("请输入");
        builder.setView(linearLayout);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                onInputCompletedListener.onInputCompleted(new String[]{textInputLayout.getEditText().getText().toString(),textInputLayout1.getEditText().getText().toString()},new TextInputLayout[]{textInputLayout,textInputLayout1});
            }
        });
        builder.setNegativeButton("取消", null);
        InputMethodTool.showInputMethod(context,textInputLayout.getEditText());
        builder.show();
    }

    /**
     * 输入密码对话框
     * @param title 标题
     * @param hint 提示
     * @param onInputCompletedListener 输入完成监听
     */
    public void showInputPassword(String title, String hint, final OnInputCompletedListener onInputCompletedListener) {
        init();
        final TextInputLayout textInputLayout = getEditText();
        textInputLayout.setHint(hint);
        textInputLayout.getEditText().setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
        builder.setView(textInputLayout);
        builder.setPositiveButton(defaultOKTip, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                onInputCompletedListener.onInputCompleted(new String[]{textInputLayout.getEditText().getText().toString()}, new TextInputLayout[]{textInputLayout});
            }
        });
        builder.show();
    }

    /**
     *修改密码对话框
     * @param title 对话框标题
     * @param hint1 提示一
     * @param hint2 提示二
     * @param hint3 提示三
     * @param onInputCompletedListener 输入完毕监听
     */
    public void showModifyPassword(String title, String hint1,String hint2,String hint3, final OnInputCompletedListener onInputCompletedListener) {
        init();
        final TextInputLayout textInputLayout = getEditText();
        textInputLayout.setHint(hint1);
        final TextInputLayout textInputLayout1 = getEditText();
        textInputLayout1.setHint(hint2);
        final TextInputLayout textInputLayout2 = getEditText();
        textInputLayout2.setHint(hint3);

        textInputLayout.getEditText().setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
        textInputLayout1.getEditText().setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
        textInputLayout2.getEditText().setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);

        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(textInputLayout);
        linearLayout.addView(textInputLayout1);
        linearLayout.addView(textInputLayout2);

        builder.setTitle("请输入");
        builder.setView(linearLayout);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                onInputCompletedListener.onInputCompleted(new String[]{textInputLayout.getEditText().getText().toString(),textInputLayout1.getEditText().getText().toString(),textInputLayout2.getEditText().getText().toString()},new TextInputLayout[]{textInputLayout,textInputLayout1,textInputLayout2});
            }
        });
        builder.setNegativeButton("取消", null);
        InputMethodTool.showInputMethod(context,textInputLayout.getEditText());
        builder.show();
    }

    /**
     * 登录对话框
     * @param title 标题
     * @param hint1 账号提示
     * @param hint2 密码提示
     * @param onInputCompletedListener 输入完成监听
     */
    public void showLoginDialog(String title,String hint1,String hint2,final OnInputCompletedListener onInputCompletedListener) {
        init();

        final TextInputLayout textInputLayout = getEditText();
        textInputLayout.setHint(hint1);
        final TextInputLayout textInputLayout1 = getEditText();
        textInputLayout1.setHint(hint2);
        textInputLayout1.getEditText().setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);

        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(textInputLayout);
        linearLayout.addView(textInputLayout1);
        builder.setTitle("请输入");
        builder.setView(linearLayout);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                onInputCompletedListener.onInputCompleted(new String[]{textInputLayout.getEditText().getText().toString(),textInputLayout1.getEditText().getText().toString()},new TextInputLayout[]{textInputLayout,textInputLayout1});
            }
        });
        builder.setNegativeButton("取消", null);
        InputMethodTool.showInputMethod(context,textInputLayout.getEditText());
        builder.show();
    }


    /**
     * 获取一个md风格的TextInputLayout
     * @return
     */
    public TextInputLayout getEditText() {
        TextInputLayout textInputLayout = new TextInputLayout(context);
        TextInputEditText textInputEditText = new TextInputEditText(context);
        AppBarLayout.LayoutParams layoutParams;
        if (textInputEditText.getLayoutParams() == null) {
            layoutParams = new AppBarLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(DisplayTool.dip2px(context,DIALOG_EDITTEXU_MARGIN_LEFT),0,DisplayTool.dip2px(context,DIALOG_EDITTEXU_MARGIN_RIGHT),0);
            textInputEditText.setLayoutParams(layoutParams);
        }
        textInputLayout.addView(textInputEditText);
        return textInputLayout;
    }
}
